<?php
/**
 * Created by PhpStorm.
 * User: wx:wenyi2048
 * Date: 2022/3/31 0031
 * Time: 18:28
 */

namespace app\common\service;
/*用户服务条款*/

use think\Db;

class OptionsService extends Service
{
    protected $tableName = 'sms_mk_services';

    public function searchPage(&$view,$params,$isdelete = 0){

        $this->db->alias('s')
            ->join('sms_mk_options o','o.id = s.sms_options_id','right')
            ->field('s.*,o.key,o.value as options_key,o.sort,o.id as sms_options_id')
            ->order('o.sort','asc');
        $storeId = $this->checkStoreSelect(@$params['store_id']);
        if($storeId) $this->db->where('s.sms_stores_id','=',$storeId);
        $this->pageList($view,$params);
    }

    public function getService($storeId){
        $service = $this->db
            ->alias('s')
            ->join('sms_mk_options o','o.id = s.sms_options_id','right')
            ->where('s.sms_stores_id','=',$storeId)
            ->order('o.sort','asc')
            ->field('s.*')
            ->select();
        $option = Db::table('sms_mk_options')->select();
        foreach ($service as $s){
            foreach ($option as $o=>$op){
                if($s['sms_options_id'] == $op['id']){
                    $option[$o]['store_value'] = $s['value'];
                    continue;
                }
            }
        }

        return $option;
    }

    public function getOptionService($optionId,$storeId){
        $service = $this->db
            ->alias('s')
            ->join('sms_mk_options o','o.id = s.sms_options_id')
            ->where('sms_options_id','=',$optionId)
            ->where('sms_stores_id','=',$storeId)
            ->field('s.*,o.key')
            ->order('o.sort','asc')
            ->find();
        if($service){
            return $service;
        }else{
            $option = Db::table('sms_mk_options')
                ->where('id','=',$optionId)
                ->field('id as sms_options_id,value,key')
                ->order('sort','asc')
                ->find();
            $option['sms_stores_id'] = $storeId;
            $result = $option;
            unset($option['key']);
            $result['id'] = Db::table('sms_mk_services')->insertGetId($option);
            return $result;
        }
    }

    public function setService($optionId,$storeId,$value){
        $ret = $this->db
            ->where('sms_options_id','=',$optionId)
            ->where('sms_stores_id','=',$storeId)
            ->update(['value'=>$value]);
        return $ret;
    }





    public function searchPage2(&$view,$params,$isdelete = 0){
        $this->setDb('sms_mk_options');
        $this->db->order('sort','asc');
        $this->pageList($view,$params);
    }
}